﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using GameJolt.Async;
using GameJolt.Requests;
using Newtonsoft.Json;
#endregion

namespace GameJolt
{
	/// <summary>
	/// 
	/// </summary>
	[JsonObject]
	public sealed class Score
	{
		#region Variables
		#endregion

		#region Properties
		[JsonProperty("score")]
		public string Value
		{
			get;
			set;
		}

		[JsonProperty("sort")]
		public string Sort
		{
			get;
			set;
		}

		[JsonProperty("extra_data")]
		public string ExtraData
		{
			get;
			set;
		}

		[JsonProperty("user")]
		public string User
		{
			get;
			set;
		}

		[JsonProperty("user_id")]
		public string UserID
		{
			get;
			set;
		}

		[JsonProperty("guest")]
		public string Guest
		{
			get;
			set;
		}

		[JsonProperty("stored")]
		public string Stored
		{
			get;
			set;
		}
		#endregion

		#region Constructors
		public Score()
		{
		}
		#endregion

		#region Methods
		public static IAsyncResult BeginGetScores(string gameID, string privateKey, string username, string userToken, int limit, string tableID, AsyncCallback callback = null, object asyncState = null)
		{
			var request = ScoreRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, limit, tableID);
			return request.Begin(callback, asyncState);
		}

		public static Score[] EndGetScores(IAsyncResult result)
		{
			var joltResult = (AsyncResult<Score[]>)result;
			var request = (JsonRequest<Score[], ScoreRequestFactory.ScoreRequestResult>)joltResult.CoreData;
			return request.End(result);
		}

		public static Score[] GetScores(string gameID, string privateKey, string username, string userToken, int limit, string tableID)
		{
			var request = ScoreRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, limit, tableID);
			return request.Process(null);
		}

		public static IAsyncResult GetScores(string gameID, string privateKey, string username, string userToken, int limit, string tableID, Action<Score[]> callback)
		{
			return BeginGetScores(gameID, privateKey, username, userToken, limit, tableID, (result) => callback(EndGetScores(result)));
		}

		public static IAsyncResult BeginAddScore(string gameID, string privateKey, string score, int sort, string username, string userToken, string guest, string extraData, string tableID, AsyncCallback callback = null, object asyncState = null)
		{
			var request = ScoreRequestFactory.CreateAddRequest(gameID, privateKey, score, sort, username, userToken, guest, extraData, tableID);
			return request.Begin(callback, asyncState);
		}

		public static string EndAddScore(IAsyncResult result)
		{
			var joltResult = (AsyncResult<string>)result;
			var request = (StringRequest)joltResult.CoreData;
			return request.End(result);
		}

		public static string AddScore(string gameID, string privateKey, string score, int sort, string username, string userToken, string guest, string extraData, string tableID)
		{
			var request = ScoreRequestFactory.CreateAddRequest(gameID, privateKey, score, sort, username, userToken, guest, extraData, tableID);
			return request.Process(null);
		}

		public IAsyncResult AddScore(string gameID, string privateKey, string score, int sort, string username, string userToken, string guest, string extraData, string tableID, Action<string> callback)
		{
			return BeginAddScore(gameID, privateKey, score, sort, username, userToken, guest, extraData, tableID, (result) => callback(EndAddScore(result)));
		}
		#endregion
	}
}
